package com.qf;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.model.Article;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class test03 {
    @Autowired
    private TransportClient transportClient;
    @Autowired
    private ObjectMapper objectMapper;


    @Test
    public void demo01() throws Exception {
        transportClient.admin().indices().prepareCreate("blog06").get();

        //2.创建映射
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("article")
                .startObject("properties")
                .startObject("id")
                .field("type", "long").field("store", "true")
                .endObject()
                .startObject("title")
                .field("type", "text").field("analyzer", "ik_smart").field("store", "true")
                .endObject()
                .startObject("content")
                .field("type", "text").field("analyzer", "ik_smart").field("store", "true")
                .endObject()
                .endObject()
                .endObject()
                .endObject();

        PutMappingRequest mapping = new PutMappingRequest("blog06").type("article").source(builder);

        transportClient.admin().indices().putMapping(mapping).get();
    }


    @Test
    public void demo03() throws Exception{

        BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
        long start = System.currentTimeMillis();
        for (long i = 100000; i < 100100; i++) {
            Article article = new Article();
            article.setTitle("华为手机elasticseacrh"+i);
            article.setContent("华为手机，技术创新，值得信赖"+i);
            article.setId(i);

            String s = objectMapper.writeValueAsString(article);

            IndexRequest indexRequest = new IndexRequest("blog06", "article", "" + i).source(s, XContentType.JSON);

            bulkRequestBuilder.add(indexRequest);
        }
        BulkResponse bulkResponse = bulkRequestBuilder.get();
        long end = System.currentTimeMillis();
        System.out.println("消耗了:"+(end-start)/1000);


        System.out.println("获取状态：" + bulkResponse.status());
        if (bulkResponse.hasFailures()) {
            System.out.println("还有些--->有错误");
        }
    }





    @Test
    public void demo02(){
        SearchResponse searchResponse = transportClient.prepareSearch("blog06")
                .setTypes("article")
                //.setQuery(QueryBuilders.matchAllQuery())
                .setQuery(QueryBuilders.queryStringQuery("为手").field("content"))
                .setSource(SearchSourceBuilder.searchSource().size(100)).get();

        SearchHits hits = searchResponse.getHits();
        System.out.println("命中数"+hits.getTotalHits());

        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        
    }

    @Test
    public void termQuery(){
        SearchResponse searchResponse = transportClient.prepareSearch("blog06")
                .setTypes("article")
                .setQuery(QueryBuilders.termQuery("title", "手机"))
                .get();

        SearchHits hits = searchResponse.getHits();
        System.out.println("命中数"+hits.getTotalHits());

        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }

    }

    @Test
    public void matchQuery(){
        SearchResponse searchResponse = transportClient.prepareSearch("blog06")
                .setTypes("article")
                .setQuery(QueryBuilders.matchQuery("content", "值得信赖9"))
                .get();


        SearchHits hits = searchResponse.getHits();
        System.out.println("命中数"+hits.getTotalHits());

        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    //多字段匹配查询
    @Test
    public void multiMatchQuery(){
        SearchResponse searchResponse = transportClient.prepareSearch("blog06")
                .setTypes("article")
                .setQuery(QueryBuilders.multiMatchQuery("值得信赖", "content", "title"))
                .get();

        SearchHits hits = searchResponse.getHits();
        System.out.println("命中数"+hits.getTotalHits());


        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

    //模糊搜索: 也叫通配符搜索
    //? 表示任意字符 一定占用一个字符空间，相当于占位符
    //* 表示任意字符 可以占用也可以不占用
    @Test
    public void wildcardQuery(){



    }


}
